package com.kzw.oa.byxt.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kzw.core.service.hibernate.DefaultEntityManager;
import com.kzw.oa.byxt.model.CheckMember;
import com.kzw.oa.byxt.model.Subject;

@Service
@Transactional
public class CheckMemberService extends DefaultEntityManager<CheckMember, Long> {
	
	@Autowired
	private SubjectService subjectService;

	public void saveOrUpdate(CheckMember member, String subjectIds) {
		saveOrUpdate(member);
		// 先移除审题人原先设置的题目
		String hql = "update Subject set checkMember=null where checkMember=?";
		entityDao.batchExecute(hql, member);
		
		if (StringUtils.isNotEmpty(subjectIds)) {
			String[] uIds = subjectIds.split(",");
			for (String id : uIds) {
				if(StringUtils.isNotBlank(id)) {
					// 将题目的审题人设置成当前member中的人
					Subject subject = subjectService.get(Long.parseLong(id));
					subject.setCheckMember(member);
				}
			}
		}
	}

	/**
	 * 删除审题人时，级联更新题目的状态
	 * */
	public void removeCascade(String[] ids) {
		for(String id : ids) {
			String hql = "update Subject set checkMember=null where checkMember.oid=?";
			entityDao.batchExecute(hql, Long.parseLong(id));
			
			// 删除这个对象
			remove(Long.parseLong(id));
		}
	}

}
